package com.boot.unity.api.wx.task;

import cn.hutool.json.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.boot.unity.api.wx.entity.Stu;
import com.boot.unity.api.entity.Student;
import com.boot.unity.api.wx.service.*;
import com.github.pagehelper.util.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;


@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling   // 2.开启定时任务
public class SendJsTask {

    @Value("${serv.apiUrl}")
    private String apiUrl;

    @Value("${serv.servurl}")
    private String servurl;


    @Value("${province.secret}")
    private String secret;

    @Autowired
    private SendJsService sendJsService;

    @Autowired
    private CommonUtilUrlService commonUtilUrlService;

    @Autowired
    private StudentSyncService studentSyncService;

    @Autowired
    private StudentSyncServiceTF studentSyncServiceTF;

    @Autowired
    private StudentSyncServiceBGCX studentSyncServiceBGCX;


//     @Scheduled(fixedRate=30*1000)
    public void createJxManagerCode() throws Exception{
        System.out.println("-------------进行定时推送数据---------------");
        // 获取需要推送的学员基本数据
        String jcUrl = apiUrl+"/jsapi/cardNoList";
        String stuUrl = apiUrl+"/jsapi/stuDetail";
        String studentUrl = apiUrl+"/jsapi/getByStudentId";
        String allStuUrl = apiUrl+"/jsapi/upStuJsStatus";
        String getSchoolNumberUrl = apiUrl+"/jsapi/getSchoolNumber";


        String listStr = sendJsService.cardNolist(jcUrl);
        if(!StringUtil.isEmpty(listStr)){
            JSONArray jsonArray = new JSONArray(listStr);
            for(Object str:jsonArray){
                // 获取证件号码
                String cardStr = (String) str;
                System.out.println("进行推送的证件号码：：：：："+cardStr);
                // 获取学员信息
                JSONObject stuJson = new JSONObject();
                stuJson.put("cardNo",cardStr);
                String stuStr = sendJsService.getStuByCardNo(stuUrl, stuJson.toString());
                Stu stuEntity = JSONObject.parseObject(stuStr, Stu.class);

                // 获取驾校信息Dept
                JSONObject studentJson = new JSONObject();
                studentJson.put("studentId",stuEntity.getStudentId());
                String studentStr = commonUtilUrlService.post(studentUrl,studentJson.toString());
                System.out.println(studentStr);
                Student studentEntity = JSONObject.parseObject(studentStr, Student.class);


                // 推送中寰时的dept的schoolnumber的获取
                JSONObject schoolIdJson = new JSONObject();
                schoolIdJson.put("schoolId",stuEntity.getSchoolId());
                String schoolNumber = commonUtilUrlService.post(getSchoolNumberUrl,schoolIdJson.toString());
//                schoolNumber = "1773539168613863";
                // 进行推送学员数据
                int num = studentSyncService.studentSync(stuEntity,studentEntity,schoolNumber);
                System.out.println("推送结果(1为推送成功)："+num);
                if(num == 1) { // 数据推送成功，更新基础数据
                    JSONObject allStuJson = new JSONObject();
                    allStuJson.put("studentId", stuEntity.getStudentId());
                    allStuJson.put("cardNo", stuEntity.getCardNo());
                    allStuJson.put("schoolId", stuEntity.getSchoolId());
                    System.out.println("推送成功回调地址：："+allStuUrl);
                    commonUtilUrlService.post(allStuUrl, allStuJson.toString());
                }




            }
        }
    }



    // 退费
//     @Scheduled(fixedRate=10*60*1000)
    public void tuifei() throws Exception{
        System.out.println("-----------------------每五分钟执行一次退费---------------------------");
        JSONObject json = new JSONObject();
        String tfurl = servurl+"/admin/settlementApply/queryStuReturn";
        json.put("key","ACDF287C53D14B089907ABC8ADA8C963");
        // 获取需要推送的学员基本数据
//        String jcUrl = apiUrl+"/jsapi/cardNoList";
        String stuUrl = apiUrl+"/jsapi/stuDetail";
        String studentUrl = apiUrl+"/jsapi/getByStudentId";
//        String allStuUrl = apiUrl+"/jsapi/upStuJsStatus";
        String getSchoolNumberUrl = apiUrl+"/jsapi/getSchoolNumber";


        System.out.println("请求地址："+tfurl);
        System.out.println("请求参数："+json.toString());
        String listStr = commonUtilUrlService.post(tfurl, json.toString());
         System.out.println("请求结果："+listStr);
        if(StringUtils.isEmpty(listStr)){
            return;
        }
        com.alibaba.fastjson.JSONArray jsonArray = com.alibaba.fastjson.JSONArray.parseArray(listStr);//new JSONArray(listStr);
        if(jsonArray.size() > 0){
            for(Object obj:jsonArray){
                JSONObject tfStuJson = (JSONObject)obj;
                // 获取证件号码
                String cardStr = tfStuJson.getString("card_no");//(String) str;
                System.out.println("进行推送的证件号码：：：：："+cardStr);
                // 获取学员信息
                JSONObject stuJson = new JSONObject();
                stuJson.put("cardNo",cardStr);
                String stuStr = sendJsService.getStuByCardNo(stuUrl, stuJson.toString());
                Stu stuEntity = JSONObject.parseObject(stuStr, Stu.class);

                // 获取驾校信息Dept
                JSONObject studentJson = new JSONObject();
                studentJson.put("studentId",stuEntity.getStudentId());
                String studentStr = commonUtilUrlService.post(studentUrl,studentJson.toString());
                System.out.println(studentStr);
                Student studentEntity = JSONObject.parseObject(studentStr, Student.class);


                // 推送中寰时的dept的schoolnumber的获取
                JSONObject schoolIdJson = new JSONObject();
                schoolIdJson.put("schoolId",stuEntity.getSchoolId());
                String schoolNumber = commonUtilUrlService.post(getSchoolNumberUrl,schoolIdJson.toString());

                // 进行推送学员数据
                int num = studentSyncServiceTF.studentSync_stop(stuEntity,studentEntity,schoolNumber);// studentSyncService.studentSync(stuEntity,studentEntity,schoolNumber);
                System.out.println("推送结果(1为推送成功)："+num);
                if(num == 1) { // 数据推送成功，更新基础数据
                    json = new JSONObject();
                    json.put("key","ACDF287C53D14B089907ABC8ADA8C963");
                    json.put("id",tfStuJson.getString("id"));
                    tfurl = servurl+"/admin/settlementApply/jsStuReturn";
                    String objResult = commonUtilUrlService.post(tfurl, json.toString());
                    System.out.println("运行结果："+objResult);
                }



            }
        }

    }


    // 转校


    // 变更车型
//     @Scheduled(fixedRate=10*60*1000)
    public void changeTrainType() throws Exception{
        System.out.println("-----------------------每五分钟执行一次变更车型---------------------------");
        JSONObject json = new JSONObject();
        String chturl = servurl+"/admin/carModel/queryStuChange";
        json.put("key",secret);
        // 获取需要推送的学员基本数据
        String jcUrl = apiUrl+"/jsapi/cardNoList";
        String stuUrl = apiUrl+"/jsapi/stuDetail";
        String studentUrl = apiUrl+"/jsapi/getByStudentId";
        String allStuUrl = apiUrl+"/jsapi/upStuJsStatus";
        String getSchoolNumberUrl = apiUrl+"/jsapi/getSchoolNumber";

        String listStr = commonUtilUrlService.post(chturl, json.toString());
        if(StringUtils.isEmpty(listStr)){
            return;
        }
        com.alibaba.fastjson.JSONArray jsonArray = com.alibaba.fastjson.JSONArray.parseArray(listStr);
        if(jsonArray.size() > 0){
            for(Object obj:jsonArray){
                JSONObject tfStuJson = (JSONObject)obj;
                // 获取证件号码
                String cardStr = tfStuJson.getString("card_no");//(String) str;
                System.out.println("进行推送的证件号码：：：：："+cardStr);
                // 获取学员信息
                JSONObject stuJson = new JSONObject();
                stuJson.put("cardNo",cardStr);
                String stuStr = sendJsService.getStuByCardNo(stuUrl, stuJson.toString());
                Stu stuEntity = JSONObject.parseObject(stuStr, Stu.class);

                // 获取驾校信息Dept
                JSONObject studentJson = new JSONObject();
                studentJson.put("studentId",stuEntity.getStudentId());
                String studentStr = commonUtilUrlService.post(studentUrl,studentJson.toString());
                System.out.println(studentStr);
                Student studentEntity = JSONObject.parseObject(studentStr, Student.class);


                // 推送中寰时的dept的schoolnumber的获取
                JSONObject schoolIdJson = new JSONObject();
                schoolIdJson.put("schoolId",stuEntity.getSchoolId());
                String schoolNumber = commonUtilUrlService.post(getSchoolNumberUrl,schoolIdJson.toString());

                // 进行推送学员数据
                int num = studentSyncServiceBGCX.studentSync(stuEntity,studentEntity,schoolNumber);// studentSyncService.studentSync(stuEntity,studentEntity,schoolNumber);
                System.out.println("推送结果(1为推送成功)："+num);
                if(num == 1) { // 数据推送成功，更新基础数据
                    json = new JSONObject();
                    json.put("key",secret);
                    json.put("id",tfStuJson.getString("changeId"));
                    chturl = servurl+"/admin/carModel/ctStatusRtn";
                    commonUtilUrlService.post(chturl, json.toString());
                }
            }
        }
    }
}
